package wzh000;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class CloudinessClient {
	private String host;
	private int serverPort = 42083;
	private Calendar cal;
	private SimpleDateFormat date_format;
	private Socket skt;
	private boolean hasImage;
	private double CC;
	
	

	public CloudinessClient(String host, int port) {
		this.host = host;
		this.serverPort = port;
		this.cal = new GregorianCalendar(2004, 01, 01, 00, 00);
		this.date_format = new SimpleDateFormat("yyyyMMddHHmm");
		
		try {
			this.skt = new Socket(this.host, this.serverPort);
		} catch (UnknownHostException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}
	}
	
	public boolean hasImage(){
		return this.hasImage;
	}

	public int getPort() {
		return this.serverPort;
	}

	public String getHost() {
		return this.host;
	}

	private void setHasImage(boolean b) {
		this.hasImage = b;
	}

	public void sendReq() {
		String time = generateTime(0);
		PrintWriter out = null;

		ObjectInputStream ois = null;

		try {

			out = new PrintWriter(this.skt.getOutputStream(), true);
			ois = new ObjectInputStream(this.skt.getInputStream());
			System.out.println("Requesting time:" + time);
			out.println(time);
			double cc = ois.readDouble();
			setCC(cc);
			System.out.println("Received CC is " + cc);

			BufferedOutputStream bos = new BufferedOutputStream(
					new FileOutputStream("zw.jpg"));
			InputStream inputStream = this.skt.getInputStream();
			BufferedInputStream bis = new BufferedInputStream(inputStream);
			byte[] content = new byte[1024];
			int r;
			int bytesRead = 0;
			while ((r = bis.read(content)) != -1) {
				bos.write(content, 0, r);
				bytesRead += r;
			}
			if (content[0] == '0') {
				setHasImage(false);
			}

			System.out.println("received " + bytesRead + " bytes");

			out.close();
			ois.close();
			bos.close();
			inputStream.close();
			bis.close();
			skt.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

			try {
				if (this.skt != null) {
					this.skt.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	}

	public String generateTime(int t) {

		this.cal.roll(Calendar.MINUTE, t);

		return (this.date_format.format(this.cal.getTime()));

	}

	public void close() {
		try {
			if (this.skt != null) {
				this.skt.close();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public double getCC(){
		return this.CC;
	}
	
	private void setCC(double in){
		this.CC = in;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		CloudinessClient client = new CloudinessClient("127.0.0.1", 42083);
		JFrame frame = new JFrame("My GUI");
		JPanel panel = null;
		try{
			panel = new ImageDisplayer();
		}catch(Exception ex){
			ex.printStackTrace();
		}
			
		for (int i = 0; i < 10; i++) {
			client.sendReq();
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {

				e.printStackTrace();
			}
			
			frame.setResizable(false);
			if(client.hasImage() == true){
				((ImageDisplayer) panel).load();
				frame.getContentPane().add(panel);
				frame.setSize(panel.getWidth(),panel.getHeight());
				frame.setTitle(Double.toString(client.getCC()));
				frame.setVisible(true);
				
			}else{
				System.out.format("CC: %.1f%%", client.getCC());
				frame.setVisible(false);
			}
			frame.setLocationRelativeTo(null);
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			
			client.generateTime(15);
		}
		client.close();

	}
	


	

}
